package com.ctcf.ocr.engine.impl.controller;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctcf.ocr.engine.api.entity.OcrResponse;
import com.ctcf.ocr.engine.api.service.IOcrService;
import com.ctcf.ocr.engine.impl.utils.HttpRequestUtils;

@Controller
@RequestMapping("/engine")
public class OcrController {

	private final static Logger logger = LoggerFactory.getLogger(OcrController.class);

	@Resource
	private IOcrService ocrService;

	@RequestMapping(value = "/single/{fileName}", method = RequestMethod.GET)
	public @ResponseBody OcrResponse recongizeImageSingle(@PathVariable String fileName) {
		logger.info("文件名：{}", fileName);
		OcrResponse response = ocrService.recognizeSingle(fileName);
		if (response == null) {
			response = new OcrResponse();
			response.setName(fileName);
		}
		return response;
	}

	@RequestMapping(value = "/batch", method = RequestMethod.GET)
	public @ResponseBody List<OcrResponse> recongizeImageBatch() {
		List<OcrResponse> lstResult = ocrService.recognizeBatch();
		return lstResult;
	}

	@RequestMapping(value = "/convert/tif", method = RequestMethod.GET)
	public @ResponseBody List<OcrResponse> convertImageToTif() {
		List<OcrResponse> lstResult = ocrService.convertImageToTif();
		return lstResult;
	}

	@RequestMapping(value = "/image/{count}", method = RequestMethod.GET)
	public @ResponseBody String downloadBatchImage(@PathVariable int count) {
		HttpRequestUtils.downloadPbcVerifyCode(count);
		return "success";
	}

	@RequestMapping(value = "/pbc/image", method = RequestMethod.GET)
	public @ResponseBody OcrResponse fetchImage() {
		OcrResponse response = ocrService.fetchImageFromPbc();
		return response;
	}
	
	@RequestMapping(value = "/test/{msg}", method = RequestMethod.GET)
	public @ResponseBody String test(@PathVariable String msg) {
		return msg;
	}

}
